package com.jiyinit.web.core.business.service;

import com.jiyinit.web.core.business.dao.TabCoreUserDao;
import com.jiyinit.web.core.business.dao.TabFriendGroupDao;
import com.jiyinit.web.core.business.dao.UserFriendDao;
import com.jiyinit.web.core.business.dto.FriendGroupRequest;
import com.jiyinit.web.core.business.dto.FriendRequest;
import com.jiyinit.web.core.business.entity.TabFriendGroupEntity;
import com.jiyinit.web.core.business.entity.TabUserFriendEntity;
import com.jiyinit.web.core.business.enums.CommonEnum;
import com.jiyinit.web.core.common.util.MessagePush;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by changly on 2017/12/6.
 */
@Service("core.friendServiceImpl")
public class UserFriendServiceImpl {

    @Resource(name ="core.tabCoreUserDao")
    private TabCoreUserDao tabCoreUserDao;

    @Resource(name = "core.userFriend")
    private UserFriendDao userFriendDao;

    @Resource(name = "core.friendGroupDao")
    private TabFriendGroupDao friendGroupDao;

    @Resource(name ="core.jpushClientServiceImpl")
    private JpushClientServiceImpl jpushClientService;

    public List<TabUserFriendEntity> findFriends(Integer userId){
//       return tabCoreUserDao.findFriends(userId);
        return userFriendDao.findFriends(userId);
    }
    public List<TabUserFriendEntity> findAppFriends(Integer userId){
//       return tabCoreUserDao.findFriends(userId);
        return userFriendDao.findAppFriends(userId);
    }

    public int addFriend(FriendRequest friendRequest){
        TabUserFriendEntity entity = new TabUserFriendEntity();
        entity.setUserId(friendRequest.getUserId());
        entity.setFriendId(friendRequest.getFriendId());
        entity.setStatus(CommonEnum.FriendStatus.WAIT.getCode());
        entity.setCreateTime(new Date());
        entity.setCreateId(friendRequest.getUserId());

        TabUserFriendEntity entity1 = new TabUserFriendEntity();
        entity1.setUserId(friendRequest.getFriendId());
        entity1.setFriendId(friendRequest.getUserId());
        entity1.setStatus(CommonEnum.FriendStatus.WAIT.getCode());
        entity1.setCreateTime(new Date());
        entity1.setCreateId(friendRequest.getUserId());
        userFriendDao.addFriend(entity1);
        int num=userFriendDao.addFriend(entity);

        if(num>0)
        {
            //极光推送
            /*Map<String,String> returnStr = new HashMap<>();
            returnStr.put("createUid", String.valueOf(friendRequest.getUserId()));
            returnStr.put("userId", String.valueOf(friendRequest.getUserId()));
            returnStr.put("userName", String.valueOf(friendRequest.getUserName()));
            returnStr.put("friendId", String.valueOf(friendRequest.getFriendId()));
            returnStr.put("content", String.valueOf(friendRequest.getUserName()));
            returnStr.put("strikeType", "2");
            returnStr.put("type", "0");
            returnStr.put("dsc", String.valueOf(friendRequest.getUserName()));

            String[] pusers = new String[1];
            pusers[0] = String.valueOf(friendRequest.getFriendId());
            if(friendRequest.getChannel()!=null&&friendRequest.getChannel().equals("01")){
                MessagePush messagePush = new MessagePush(returnStr,pusers);
            }else{
                MessagePush messagePush = new MessagePush(returnStr,pusers,"andriod");
            }*/

            HashMap<String, String> returnStr = new HashMap<>();
            returnStr.put("createUid", String.valueOf(friendRequest.getUserId()));
            returnStr.put("userId", String.valueOf(friendRequest.getUserId()));
            returnStr.put("userName", String.valueOf(friendRequest.getUserName()));
            returnStr.put("friendId", String.valueOf(friendRequest.getFriendId()));
            returnStr.put("content", String.valueOf(friendRequest.getUserName()+"请求加为好友"));
            returnStr.put("strikeType", "2");
            returnStr.put("type", "0");
            returnStr.put("dsc", String.valueOf(friendRequest.getUserName()+"请求加为好友"));
            returnStr.put("taskId", "-1");

            String[] pusers = new String[1];
            pusers[0] = String.valueOf(friendRequest.getFriendId());
            jpushClientService.send(friendRequest.getChannel(), returnStr, pusers);
        }
        return num;
    }

    public List<TabUserFriendEntity> isFriend(Integer userId,Integer friendId){
        List<TabUserFriendEntity> entities =  userFriendDao.isFriend(userId,friendId);
       return entities;
    }

    public int agreeFriend(FriendRequest friendRequest){
        return userFriendDao.agreeFriend(friendRequest);
    }

    public int addFriendGroup(FriendGroupRequest request){
        TabFriendGroupEntity entity = new TabFriendGroupEntity();
        entity.setOwnId(request.getUserId());
        entity.setIsDelete(CommonEnum.IsDelete.COMMON.getCode());
        entity.setFriendGroupName(request.getFriendGroupName());
        entity.setCreateTime(new Date());
        entity.setCreateId(request.getUserId());
         friendGroupDao.add(entity);
        return entity.getFriendGroupId();
    }

    public int updateUserFriend(FriendGroupRequest groupRequest){
        return userFriendDao.update(groupRequest);
    }


    public List<TabFriendGroupEntity> findGroupInfo(Integer userId){
        return friendGroupDao.findGroupInfo(userId);
    }

    public Integer getFriendMsg(Integer userId){
        return userFriendDao.getFriendMsg(userId);
    }
}
